define(['app', 'comm/pager/PagerControl', 'js/services/MessageBox', 'js/services/MuseService',
    'js/filters/StrLimitFilter', 'js/controllers/admin/corpus/SysCorpusEditController'], function (app) {

    var deps = ['$scope', '$state', '$stateParams', '$modal', 'MessageBox', 'MuseService'];

    function controller ($scope, $state, $stateParams, $modal, MessageBox, MuseService) {
        var sysCorpusTypeId = $stateParams.sysCorpusTypeId || '';
        //初始化查询对象 和 分页对象

        $scope.queryObj = {
            corpusTypeId: sysCorpusTypeId
        };
        $scope.pagerObj = {
            pageSize: 10,
            currentPage: 1
        };

        //获取列表
        $scope.list = function () {
            //封装查询条件
            $scope.pagerObj.searchMap = $scope.queryObj;
            MuseService.post('admin/sysCorpus/list.json', $scope.pagerObj).success(
                function (data, status, headers, config) {
                    if (data.state == 0) {
                        $scope.sysCorpusList = data.data;

                        //分页器
                        $scope.pagerObj = PageCtrl().page($scope.pagerObj, data.totalPage, $scope.pagerObj.currentPage);
                    }
                });
        };

        $scope.getSysCorpusType = function () {
            //封装查询条件
            $scope.pagerObj.searchMap = $scope.queryObj;
            MuseService.post('admin/sysCorpusType/get.json', {id: sysCorpusTypeId}).success(
                function (data, status, headers, config) {
                    if (data.state == 0) {
                        $scope.typeName = data.data.name;
                    }
                });
        };

        $scope.list();
        $scope.getSysCorpusType();

        $scope.dialog = function (obj) {
            $modal.open({
                templateUrl: 'views/admin/corpus/sysCorpusEdit.html',
                controller: 'SysCorpusEditController',
                backdrop: 'static',
                keyboard: false,
                size: 'lg',
                resolve: {
                    selfObj: function () { //传递参数
                        return angular.copy(obj);
                    }

                },
                windowClass: 'app-modal-window'
            }).result.then(function () {
                $scope.list();
            });
        };

        //新增
        $scope.add = function () {
            //$state.go('sysCorpusEdit', {sysCorpusTypeId:sysCorpusTypeId});
            $scope.dialog({sysCorpusTypeId: sysCorpusTypeId});
        };

        //编辑(属性)
        $scope.edit = function (id) {
            //$state.go('sysCorpusEdit', {sysCorpusTypeId:sysCorpusTypeId,sysCorpusId: id});
            $scope.dialog({sysCorpusTypeId: sysCorpusTypeId, sysCorpusId: id});
        };

        //新增
        $scope.add = function () {
            //$state.go('sysCorpusEdit', {sysCorpusTypeId:sysCorpusTypeId});
            $scope.dialog({sysCorpusTypeId: sysCorpusTypeId});
        };

        //编辑(属性)
        $scope.edit = function (id) {
            //$state.go('sysCorpusEdit', {sysCorpusTypeId:sysCorpusTypeId,sysCorpusId: id});
            $scope.dialog({sysCorpusTypeId: sysCorpusTypeId, sysCorpusId: id});
        };

        //删除
        $scope.delete = function (id) {
            var dialog = MessageBox.confirm('确认要删除该信息吗？');
            dialog.result.then(function () {
                MuseService.post('admin/sysCorpus/delete.json', {id: id}).success(
                    function (data) {
                        if (data.state == 0) {
                            MessageBox.notify({msg: '删除成功！', displayTime: 1000});
                            $scope.list();
                        } else {
                            MessageBox.error('删除异常：' + data.message);
                        }
                    });
            });

        };

        $scope.pageChanged = function () {
            PageCtrl().check($scope.pagerObj);
            $scope.list();
        }
    }

    controller.$inject = deps;
    app.lazy.controller('SysCorpusListController', controller);
});
